package student.action;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.SimpleDateFormat;


import com.opensymphony.xwork2.ActionContext;
import com.util.DB;

import edu.student.models.Announcements;
import edu.student.models.Interest;
import edu.student.models.News;
import edu.student.models.User;

public class UpdateProfile {
	private String username="";
	private String rollNo;
	private String email;
	private Date dob;
	
	private List<News> news;
	private List<Announcements> announcements;
	private String image="";
	private Timestamp lastLogin;
	
	private ArrayList<String> interests = new ArrayList<String>();
	private ArrayList<String> selInterests = new ArrayList<String>();
	
	public String getUsername() {
		return username;
	}
	public void setUsername(String username) {
		this.username = username;
	}
	public String getRollNo() {
		return rollNo;
	}
	public void setRollNo(String rollNo) {
		this.rollNo = rollNo;
	}
	public String getEmail() {
		return email;
	}
	public void setEmail(String email) {
		this.email = email;
	}
	public Date getDob() {
		return dob;
	}
	public void setDob(Date dob) {
		this.dob = dob;
	}
		
	public ArrayList<String> getInterests() {
		return interests;
	}
	public void setInterests(ArrayList<String> interests) {
		this.interests = interests;
	}
	public ArrayList<String> getSelInterests() {
		return selInterests;
	}
	public void setSelInterests(ArrayList<String> selInterests) {
		this.selInterests = selInterests;
	}
	
		
	public List<News> getNews() {
		return news;
	}
	public void setNews(List<News> news) {
		this.news = news;
	}
	public List<Announcements> getAnnouncements() {
		return announcements;
	}
	public void setAnnouncements(List<Announcements> announcements) {
		this.announcements = announcements;
	}
	public String getImage() {
		return image;
	}
	public void setImage(String image) {
		this.image = image;
	}
	public Timestamp getLastLogin() {
		return lastLogin;
	}
	public void setLastLogin(Timestamp lastLogin) {
		this.lastLogin = lastLogin;
	}
	public String execute(){
		User user=new User();
		Map<String, Object> session = ActionContext.getContext().getSession();
		
		
		if(session.get("user") != null && session.get("user") != "")
			user = (User) session.get("user");
		session.put("user", user);
		rollNo = user.getRollNor();
		username=user.getUsername();
		image=user.getImage();
		lastLogin=user.getLastLogin();
		news = News.getNews(" where ui.roll_No='"+user.getRollNor()+"' ");
		announcements=Announcements.getAnnouncements(" where ui.roll_No='"+user.getRollNor()+"' ");
		
		System.out.println("\nIN EXECUTE OF UPDATEPROFILE\n");
		System.out.println("\n dob in execute is "+dob);
		System.out.println("\nin update fn");
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");		
		String query = "UPDATE USER SET USER_NAME='" +this.username + "', " + "EMAIL_ID='" + this.email + "', " +
		"DOB='" + formatter.format(dob) + "' WHERE ROLL_NO='" + this.rollNo +"'";
		System.out.println(query);
		DB.update(query);
		
		UpdateProfile.deleteAllUserInterests(rollNo);
		String interest;
		
		for(int i=0; i<interests.size(); i++){
		   interest = interests.get(i);
		   UpdateProfile.insertUserInterest(rollNo, interest);			
		}
		
		for(int i=0; i<selInterests.size(); i++){
		    interest = selInterests.get(i);
		    UpdateProfile.insertUserInterest(rollNo, interest);			
		}
				
		return "success";
	}
	
	public static ArrayList<String> findNotSelectedInterests(String rollNo){
		
		ArrayList<String> interests = new ArrayList<String>();
		ResultSet result = null;
		
		String query = "select interest_name from interest where interest_id not in (select interest_id from user_interests where roll_no ='" + rollNo + "')";
		System.out.println(query);
		Connection connection = DB.getConnection();
		result = DB.readFromDB(query, connection);
		try {
			while (result.next()) {
				interests.add(result.getString("interest_name"));
			}
		} catch (SQLException e) {
	       System.out.println("Exception while reading from db"+ e);
		}
		DB.close(connection);
		return interests;
	}
	
	public static void deleteAllUserInterests(String rollNo){
		
		String query = "delete from user_interests where roll_no ='" + rollNo + "'";
		DB.update(query);
		
	}
	
	public static void insertUserInterest(String rollNo, String interest_name){
		
		String query1 = "select interest_id from interest where interest_name = '" + interest_name + "'";
		int interest_id = 0;
		Connection connection = DB.getConnection();
		ResultSet result = null;
		result = DB.readFromDB(query1, connection);
		
		try{
			while(result.next()){
			 interest_id = result.getInt("interest_id");	
			}
		   }
			catch(Exception ex){
				System.out.println("Exception while reading from db"+ ex);
			}
		
		String query2 = "insert into user_interests(interest_id, roll_no) values(" + interest_id + ", '" + rollNo + "')";
		DB.update(query2);
		
		DB.close(connection);
		
		}
		
	}
	

